package v1

import (
	"gin_cli/models"
	"gin_cli/pkg/e"
	"gin_cli/pkg/lgz"
	"gin_cli/pkg/util"
	"gin_cli/service"
	"net/http"
	"strconv"
	"strings"

	"github.com/gin-gonic/gin"
)

var token = 68

type Member struct {
}

const DEFAULT_TABLE = "merchant,device,store"

func (this *Member) GetInfo(c *gin.Context) {
	token := c.GetInt64("id")
	//fmt.Println(_type, "social", social)
	deviceNo := c.DefaultQuery("device_number", "")
	member := service.GetMemberUserInfo(token)
	mcoin := service.GetMemberMcoin(token, deviceNo)
	data := util.GetStructTagJson(&member, member)
	data["mcoin"] = mcoin
	c.JSON(
		http.StatusOK,
		e.GetResponse(e.SUCCESS, data),
	)
}

func (this *Member) GetPayOrderList(c *gin.Context) {
	token := c.GetInt64("id")
	pagination := util.GetPagination(c)
	productType := c.DefaultQuery("product_type", "1,2")
	status := c.DefaultQuery("status", "1")
	payType := c.DefaultQuery("pay_type", "1,2")
	where := map[string]interface{}{
		"member_id":    token,
		"product_type": strings.Split(productType, ","),
		"pay_type":     strings.Split(payType, ","),
		"status":       strings.Split(status, ","),
	}

	rows, count := new(models.PayOrder).GetListByMemberID(where, pagination)

	data := map[string]interface{}{
		"rows":      rows,
		"count":     count,
		"page":      pagination["page"],
		"page_size": pagination["pageSize"],
	}
	c.JSON(
		http.StatusOK,
		e.GetResponse(e.SUCCESS, data),
	)
}
func (this *Member) GetPayOrderByPayOrderID(c *gin.Context) {
	idStr := c.Param("id")
	idInt, err := strconv.Atoi(idStr)
	if err != nil {
		c.JSON(400, e.GetResponse(e.INVALID_PARAMS, ""))
	}
	data := new(models.PayOrder).GetById(idInt, token)
	if data.ID == 0 {
		c.JSON(http.StatusOK, e.GetResponse(e.INVALID_PARAMS, ""))
		c.Abort()
		return
	}
	c.JSON(http.StatusOK, e.GetResponse(e.SUCCESS, data))
}

func (this *Member) GetCoinLogList(c *gin.Context) {
	pagination := util.GetPagination(c)
	orderType := c.DefaultQuery("order_type", "1,2,3")
	status := c.DefaultQuery("status", "1")
	_type := c.DefaultQuery("type", "1,2,3")
	where := map[string]interface{}{
		"member_id":  token,
		"order_type": strings.Split(orderType, ","),
		"type":       strings.Split(_type, ","),
		"status":     strings.Split(status, ","),
	}
	table := c.DefaultQuery("table", DEFAULT_TABLE)
	rows, count := new(models.CoinLog).GetListByMemberID(
		where,
		pagination,
		table,
	)

	data := map[string]interface{}{
		"rows":      rows,
		"count":     count,
		"page":      pagination["page"],
		"page_size": pagination["pageSize"],
	}
	c.JSON(http.StatusOK, e.GetResponse(e.SUCCESS, data))
}

func (this *Member) GetCoinLogByID(c *gin.Context) {
	idStr := c.Param("id")
	idInt, err := strconv.Atoi(idStr)
	if err != nil {
		c.JSON(400, e.GetResponse(e.INVALID_PARAMS, ""))
	}
	table := c.DefaultQuery("table", DEFAULT_TABLE)
	where := map[string]interface{}{
		"member_id": token,
		"id":        idInt,
	}
	data := new(models.CoinLog).GetById(where, table)
	c.JSON(http.StatusOK, e.GetResponse(e.SUCCESS, data))
}

func (this *Member) GetPrizeList(c *gin.Context) {
	pagination := util.GetPagination(c)
	where := map[string]interface{}{
		"member_id": token,
	}
	rows, count := new(models.LogDeviceFallMember).GetList(where, pagination)
	data := map[string]interface{}{
		"rows":      rows,
		"count":     count,
		"page_size": pagination["pageSize"],
		"current":   pagination["current"],
	}
	c.JSON(http.StatusOK, e.GetResponse(e.SUCCESS, data))
}

func (this *Member) GetLogSupplementList(c *gin.Context) {
	pagination := util.GetPagination(c)
	where := map[string]interface{}{
		"member_id": token,
	}
	rows, count := new(models.LogSupplement).GetList(where, pagination)
	data := map[string]interface{}{
		"rows":      rows,
		"count":     count,
		"page_size": pagination["pageSize"],
		"current":   pagination["current"],
	}
	c.JSON(http.StatusOK, e.GetResponse(e.SUCCESS, data))
}

func (this *Member) GetUserQrcode(c *gin.Context) {
	accessToken := service.GetLgzAccessToken()
	userQrcode, err := lgz.LgzSDK.GetUserQrcode(accessToken, strconv.Itoa(token))
	if err != nil {
		panic(err)
	}

	c.JSON(http.StatusOK, e.GetResponse(e.SUCCESS, userQrcode))
}
